package cn.turing.firecontrol.device.alarm.task;


import ch.qos.logback.core.net.SyslogOutputStream;
import cn.turing.firecontrol.device.alarm.config.HttpUtil;
import cn.turing.firecontrol.device.alarm.entity.Sensor;
import cn.turing.firecontrol.device.alarm.read.ModbusRtuRead;
import cn.turing.firecontrol.device.alarm.service.ISensorService;
import cn.turing.firecontrol.device.alarm.service.impl.WebSocketServer;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

@Slf4j
@Component
public class ReadTask {

    @Autowired
    private ModbusRtuRead modbusRtuRead;

    @Autowired
    private ISensorService iSensorService;
    private CopyOnWriteArraySet<WebSocketServer> webSocketset = WebSocketServer.getWebSocketSet();
    /**
     * 执行完成后，间隔1秒再次读取设备数据
     */
   // @Scheduled(fixedDelay = 10000)
    @XxlJob("demoJobHandler")
    public void read() {
        try {
            String read = modbusRtuRead.read();
            //判断设备数据，是否检测到烟感，如果有，则报警
            Sensor sensor = iSensorService.selectDeviceStaTusBySensorNo("COM4");
//             && !equals("")

            if(read.compareTo("0.12") > 0){
                System.out.println("1++++++++++报警");

                  /*  Map<String, String> params = new HashMap<>(2);
                    //访问消息推送服务地址
                    String url = "http://localhost:9002/push";

                    params.put("sensorNo", "o2QYW6uEkkoDgaKGHMu2Rt3Kq2PE");

                    params.put("message", "紧急情况，当前该设备编号:"+sensor.getSensorNo()+"报警，请及时处理！");

                    HttpUtil.post(url, params);*/

                if(!sensor.getStatus().equals("1")) {
                    iSensorService.setDeviceStatusBySensorNo("COM4", 1);
                }
                webSocketset.forEach(data->{
                    try {
                        //这里使用的10以内的随机数，前提是保证数据库查到的数据不少于10条
//                            data.sendMessage(read+"");
                        Thread.sleep(3000);//没s发一次
                        data.sendMessage(10+"");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
            }else if(read.compareTo("0.12") == 0){
                if(!sensor.getStatus().equals("2")) {
                    System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
                    iSensorService.setDeviceStatusBySensorNo("COM4", 2);


                    /*Map<String, String> params = new HashMap<>(2);
                    //访问消息推送服务地址
                    String url = "http://10.39.42.5:9090/ex/send/message";

                    params.put("sensorNo", "o2QYW6uEkkoDgaKGHMu2Rt3Kq2PE");

                    params.put("message", "当前该设备编号:"+sensor.getSensorNo()+"该设备已恢复正常状态！");

                    HttpUtil.post(url, params);*/
                }
                System.out.println("2++++++++++正常");


                webSocketset.forEach(data->{

                    try {
                        //这里使用的10以内的随机数，前提是保证数据库查到的数据不少于10条
//                            data.sendMessage(read+"");
                        Thread.sleep(3000);//没s发一次
                        data.sendMessage(20+"");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
            }else if(read.compareTo("0.12") < 0){
                Map<String, String> params = new HashMap<>(2);
               /* //访问消息推送服务地址
                String url = "http://10.39.42.5:9090/ex/send/message";

                params.put("sensorNo", "o2QYW6uEkkoDgaKGHMu2Rt3Kq2PE");

                params.put("message", "当前该设备编号:"+sensor.getSensorNo()+"出现故障，请及时修复！");

                HttpUtil.post(url, params);*/
                if(!sensor.getStatus().equals("0")) {
                    iSensorService.setDeviceStatusBySensorNo("COM4", 0);
                }
                System.out.println("0++++++++++故障");
                webSocketset.forEach(data->{
                    try {
                        //这里使用的10以内的随机数，前提是保证数据库查到的数据不少于10条
//                            data.sendMessage(read+"");
                        Thread.sleep(3000);//没s发一次
                        data.sendMessage(30+"");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
            }



        } catch (Exception e) {
            e.printStackTrace();
            log.info("设备异常");
            //上报数据，进行异常维护
        }
    }

}
